<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索 -->
      <el-input v-model="query.value" clearable placeholder="输入岗位名称搜索" maxlength="10" style="width: 200px;"
                class="filter-item" @keyup.enter.native="toQuery"/>
      <el-select v-model="query.enabled" clearable placeholder="状态" class="filter-item" style="width: 90px"
                 @change="toQuery">
        <el-option v-for="item in enabledTypeOptions" :key="item.key" :label="item.display_name" :value="item.key"/>
      </el-select>
    </div>
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" size="small" style="width: 100%;"
              @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55"/>
      <el-table-column prop="sort" label="序号" align="center" width="100">
        <template slot-scope="scope">
          {{ scope.row.sort }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称" align="center"/>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <div v-for="item in dicts" :key="item.id">
            <el-tag v-if="scope.row.enabled.toString() === item.value" :type="scope.row.enabled ? '' : 'info'">
              {{item.label }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>
  </div>
</template>

<script>
  import initData from '@/mixins/initData'
  import initDict from '@/mixins/initDict'
  import { parseTime } from '@/utils/index'

  export default {
    mixins: [initData, initDict],
    data() {
      return {
        enabledTypeOptions: [
          { key: 'true', display_name: '正常' },
          { key: 'false', display_name: '禁用' }
        ],
        selectedData: []
      }
    },
    created() {
      this.$nextTick(() => {
        this.init()
        // 加载数据字典
        this.getDict('job_status')
      })
    },
    methods: {
      parseTime,
      beforeInit() {
        this.url = 'api/job'
        const sort = 'sort,asc'
        this.params = { page: this.page, size: this.size, sort: sort }
        const query = this.query
        const value = query.value
        const enabled = query.enabled
        if (value) {
          this.params['name'] = value
        }
        if (enabled !== '' && enabled !== null) {
          this.params['enabled'] = enabled
        }
        return true
      },
      handleSelectionChange(val) {
        this.selectedData = val
      },
      getResult() {
        const data = []
        this.selectedData.forEach(item => {
          const itemInfo = {
            id: item.id,
            name: item.name
          }
          data.push(itemInfo)
        })
        return { type: 2, data: data }
      }
    }
  }
</script>

<style scoped>

</style>
